package cool.webstudy.admin.utils.common;

import cool.webstudy.admin.constant.enums.rescode.BaseResCodeEnum;
import cool.webstudy.admin.constant.enums.rescode.CommonResCodeEnum;
import cool.webstudy.admin.exception.BaseException;
import io.swagger.v3.oas.annotations.media.Schema;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * @author think
 * @date 2024/7/15 23:07
 * @description: 统一响应模板对象
 */
public class Result<T> implements BaseResult, Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 响应状态码，默认成功
     */
    @Schema(description = "响应状态码")
    private String code = CommonResCodeEnum.RC_SUCCESS.code();
    /**
     * 执行状态
     */
    @Schema(description = "执行状态")
    private boolean success = true;
    /**
     * 响应的数据体
     */
    @Schema(description = "响应的数据体")
    private T data;
    /**
     * 异常信息
     */
    private BaseException exception;
    /**
     * 消息
     */
    @Schema(description = "消息")
    private String msg = CommonResCodeEnum.RC_SUCCESS.msg();

    /**
     * 构造方法
     */
    public Result() {
    }

    public Result(String code) {
        this.code = code;
        if (CommonResCodeEnum.RC_SUCCESS.code().equals(code)) {
            this.success = true;
        } else {
            this.success = false;
        }
        this.msg = CommonResCodeEnum.getMsgByCode(code).msg();
    }

    public Result(T data) {
        this.data = data;
    }

    public Result(String code, String msg) {
        this.code = code;
        if (CommonResCodeEnum.RC_SUCCESS.code().equals(code)) {
            this.success = true;
        } else {
            this.success = false;
        }
        this.msg = msg;
    }

    public Result(String code, String msg, BaseException be) {
        this.code = code;
        if (CommonResCodeEnum.RC_SUCCESS.code().equals(code)) {
            this.success = true;
        } else {
            this.success = false;
        }
        this.msg = msg;
        this.exception = be;
    }

    public Result(String code, T data) {
        this.code = code;
        this.data = data;
    }

    public Result(String code, T data, BaseException exception) {
        this.code = code;
        this.data = data;
        this.exception = exception;
    }

    public Result(String code, T data, String msg, BaseException exception) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.exception = exception;
    }

    public static <T> Result<T> success() {
        return new Result<>();
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }

    public static <T> Result<T> failure() {
        return new Result<>(CommonResCodeEnum.RC_FAILURE.code());
    }

    public static <T> Result<T> failure(BaseResCodeEnum resCode) {
        return new Result<>(resCode.code(), resCode.msg());
    }

    public static <T> Result<T> failure(BaseResCodeEnum resCode, BaseException be) {
        return new Result<>(resCode.code(), resCode.msg(), be);
    }
    public static <T> Result<T> failure(BaseResCodeEnum resCode,String msg, BaseException be) {
        return new Result<>(resCode.code(), msg, be);
    }

    public static <T> Result<T> failure(String code) {
        return new Result<>(code);
    }

    public static <T> Result<T> failure(String code, String msg) {
        if (StringUtils.isNotEmpty(msg)){
            return new Result<>(code, msg);
        }
        return new Result<>(code);
    }


    public void setCode(String code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public boolean getSuccess() {
        return this.success;
    }

    public BaseException getException() {
        return exception;
    }

    public void setException(BaseException exception) {
        this.exception = exception;
    }

    /**
     * 非必要不要调用，需要严格按照枚举的code进行翻译
     */
    @Deprecated
    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return msg;
    }
}
